package com.zty.xiaomiadmin.server.Service.OrderService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.qiniu.util.StringUtils;
import com.zty.xiaomiadmin.server.Dao.Good;
import com.zty.xiaomiadmin.server.Dao.OrderMapper;
import com.zty.xiaomiadmin.server.Entity.Order;
import com.zty.xiaomiadmin.server.Entity.PageResult;
import com.zty.xiaomiadmin.server.Entity.QueryPageBeanOrder;
import com.zty.xiaomiadmin.server.utils.SqlSessionUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Override
    public PageResult getOrder(QueryPageBeanOrder queryPageBean) throws IOException {
        List<Order> orderList = new ArrayList<>();
        int total = 0;
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String statusDesc = queryPageBean.getStatusDesc();
        PageHelper.startPage(currentPage, pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("currPage", (currentPage - 1) * pageSize);
        map.put("pageSize", pageSize);
        if (StringUtils.isNullOrEmpty(statusDesc)) {
            orderList = SqlSessionUtil.getNewSqlSessionInstance().getSqlSession().getMapper(OrderMapper.class).getOrders(map);
            total = SqlSessionUtil.getNewSqlSessionInstance().getSqlSession().getMapper(OrderMapper.class).getOrdersTotal(map);
        }else {
            map.put("statusDesc",statusDesc);
            orderList = SqlSessionUtil.getNewSqlSessionInstance().getSqlSession().getMapper(OrderMapper.class).getOrdersByStatusDesc(map);
            total = SqlSessionUtil.getNewSqlSessionInstance().getSqlSession().getMapper(OrderMapper.class).getOrdersByStatusDescTotal(map);
        }
        return new PageResult(total, orderList);
    }

    @Override
    public void editOrderStatusById(String orderNo) throws IOException {
        SqlSessionUtil.getSqlSessionInstance().getSqlSession().getMapper(OrderMapper.class).editOrderStatusById(orderNo);
        SqlSessionUtil.getSqlSessionInstance().getSqlSession().commit();
    }
}
